﻿using AutoMapper;
using Fabao.Shared.WebApi.Fliters;
using Member.Domain.Entities;
using Member.Domain.Services;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;

namespace tianzedianzi.Controllers
{
    public class LoginController : BaseController
    {
        private readonly ILogger<LoginController> _logger;
        private readonly AdminUserService _adminUserService;
        private readonly IMapper _mapper;
        //private readonly IRestService _rest;
        public LoginController(ILogger<LoginController> logger, IMapper mapper, AdminUserService adminUserService)
        {
            _logger = logger;
            _adminUserService = adminUserService;
            _mapper = mapper;
        }
        [NoLogin]
        [HttpGet]
        [Route("/Login/Login")]
        public ActionResult Login()
        {
            if (HttpContext.User.Identity.IsAuthenticated)
            {
                return Redirect("/Home/Index");
            }
            return View();
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="user"></param>
        /// <param name="verifyCode">验证码</param>
        /// <param name="type">登陆方式：0账号密码登录1短信验证码登录</param>
        /// <param name="identity">登陆后台：0平台1小程序</param>
        /// <returns></returns>
        [HttpPost, NoLogin]
        public async Task<IActionResult> LoginIn(AdminUser user, string verifyCode, int type = 0, int identity = 0)
        {
            //string ip = HttpHelper.GetClientIp();
            if (type == 0)
            {
                //if (HttpContext.Session.GetString("yzcode") == null || verifyCode.ToLower().Trim() != HttpContext.Session.GetString("yzcode").ToLower())
                //{
                //    return Error("验证码不正确");
                //}
                //验证是否为超级管理员
              
                if (user.isSuperAdmin(user.phone,user.password))
                {
                    user.username = "超级管理员";
                }
                else
                {
                    user = await _adminUserService.GetAdminUserByPasswordAsync(user.phone, user.password);
                    if (user == null)
                    {
                        return Error("登录失败，账号或密码不正确");
                    }
                }
            }
            else
            {
                //ExeMsgInfo smsInfo = _smsService.CheckCode(user.phone, verifyCode);//验证码校验
                //if (smsInfo.RetStatus != 200)
                //{
                //    return Error(smsInfo.RetValue);
                //}
                //user = _adminUserService.Login(user.phone, null);
                //if (user == null)
                //{
                //    return Error("登录失败");
                //}
            }

           // string browser = HttpHelper.UserAgent();
            //添加登录记录
           // await _adminUserService.InsertUserLoginLog(user.id, ip, browser);
            AdminUser auth = user;
            if (auth != null)
            {
                await HttpContext.Login(auth);
            }
            //HttpContext.Session.Remove("yzcode");
            return Success("登录成功！");
        }
        /// <summary>
        /// 退出登录
        /// </summary>
        /// <returns></returns>
        [NoLogin]
        public async Task<IActionResult> LoginOut()
        {
            //AdminUser user = HttpContext.GetAuthUser();
            await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
            return Redirect("/Login/Login");
        }
    }
}
